<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$keywords=plugin('jy_weishop')->plugin_setting('search');
$return['params']=array('jy'=>1);
if($_RQ['op']=='main'){
	//下面list,PC端需要获取商品列表
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
	);
	$searchkeyword=$keywords['keyword'];
	$return['searchkeyword']=explode(';',$searchkeyword);
	$return['order']=$_RQ['order'];
	//最近浏览
	$params=array(
		//'table'=>'jy_weishop_member_history',
		'params'=>array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),
		'fields'=>array('id','goodsid','merchid','createtime'),
		'orderby'=>'createtime desc',
		'limit'=>'5',
	);
	$hlist=pdo_getlist('jy_weishop_member_history',$params);
	if($hlist){
		foreach($hlist as &$row){
			$goods=model('jy_weishop/goods')->goods_detail($row['goodsid'],array('title','image','price'));
			$row=array_merge($goods,$row);
		}
	}
	$return['hlist']=$hlist;
	$category=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'isshow'=>1,'parentid'=>0),array(),'id','paixu desc');
	foreach($category as &$item){
		$item['childs']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>$item['id']),array(),'id','paixu desc');
	}
	//推荐分类
	$recommend=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'isshow'=>1,'isrecommand'=>1));
	$return['recommend']=$return['childs']=$recommend;
	$return['category']=$category;
	if($_RQ['pcate'] || $_RQ['pid']){
		$pcate=$_RQ['pcate']?intval($_RQ['pcate']):intval($_RQ['pid']);
		$return['params']['pcate']=$pcate;
	}
	if($_RQ['ccate'] || $_RQ['cid']){
		$ccate=$_RQ['ccate']?intval($_RQ['ccate']):intval($_RQ['cid']);
		$return['params']['ccate']=$ccate;
	}
	$return['catlevel']=2;
	$return['opencategory']=true;
	$return['feemoney']=plugin('jy_weishop')->plugin_setting('feemoney');
	set_html('title',$return['shopset']['title']?:'商品列表');
	$return['sharedata']=array(
		'title'=>$return['setting']['share_title']?'全部商品'.$return['setting']['share_title']:'全部商品-拼单商城',
		'desc'=>$return['setting']['share_desc']?$return['setting']['share_desc']:'拼单商城，一个属于自己的个人商城！',
		'link'=>custom_url('jy_newganfee/goods.index//fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$return['setting']['share_image']
	);
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
	);
	if($_RQ['tid']!=100){
		$params['timeids like']='%;'.$_RQ['tid'].';%';
		$return['params']['tid']=$_RQ['tid'];
	}else{
		$return['params']['tid']=100;
	}
	if(intval($_RQ['teamnum'])){
		$params['teamnum']=intval($_RQ['teamnum']);
		$return['params']['teamnum']=intval($_RQ['teamnum']);
	}
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','title','image','price','goodsid','credit_fee','timeids');
	$list=pdo_getslice('jy_newganfee_goods',$params,array($pindex,$psize),$total,$fields,'','star desc,id desc');
	if(!$list){
		exi('none');
	}else{
		$feemoneyset=plugin('jy_weishop')->plugin_setting('feemoney');
		$feemoneyset['feerade']=$feemoneyset['feerade']?:5;
		$fields=array('id','title','image','price','active_plugin','active_id','salenum','originalprice','usermaxbuy','unit');
		foreach($list as &$itemv){
			if($itemv['goodsid']){
				$goods=model('jy_weishop/goods')->goods_detail($itemv['goodsid'],$fields);
				$goods['originalprice']=$goods['originalprice']>0?$goods['originalprice']:price($goods['price']*1.2);
				$goods['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['goodsid']),'id');
				if($_SESSION['is_tomedia']){
					$goods['image']=$goods['image']?tomedia($goods['image']):'';
					$goods['video']=$goods['video']?tomedia($goods['video']):'';
				}
				$itemv['title']=$itemv['title']?:$goods['title'];
				$itemv['image']=$itemv['image']?:$goods['image'];
				$itemv['goodsprice']=$goods['price'];
				$itemv=array_merge($goods,$itemv);
			}
			//判断是否预约：
			$itemv['isyuyue']=pdo_getcolumn('jy_newganfee_yuyue',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pingid'=>$itemv['id']),'id');
			$itemv['isyuyue']=$itemv['isyuyue']>0?1:0;
			$itemv=$this->model('model')->up_safeprice($itemv,$return['pluginset'],true);
			if($return['pluginset']['goodstitle1']){
				$goodstitle1=$return['pluginset']['goodstitle1'];
			}else{
				$goodstitle1='{teamnum}人拼团，{lastnum}人得商品';
			}
			$goodstitle1=str_replace('{teamnum}',$itemv['teamnum'],$goodstitle1);
			$goodstitle1=str_replace('{zhong_fee}',$itemv['zhong_fee'],$goodstitle1);
			$goodstitle1=str_replace('{fan_fee}',$itemv['fan_price'],$goodstitle1);
			$goodstitle1=str_replace('{fannum}',$itemv['failnum'],$goodstitle1);
			$itemv['goodstitle1']=str_replace('{lastnum}',$itemv['lastnum'],$goodstitle1);
			if($return['pluginset']['goodstitle2']){
				$goodstitle2=$return['pluginset']['goodstitle2'];
			}else{
				$goodstitle2='{fannum}人，每人奖励{fan_fee}红包补贴';
			}
			$goodstitle2=str_replace('{fannum}',$itemv['failnum'],$goodstitle2);
			$goodstitle2=str_replace('{zhong_fee}',$itemv['zhong_fee'],$goodstitle2);
			$goodstitle2=str_replace('{teamnum}',$itemv['teamnum'],$goodstitle2);
			$goodstitle2=str_replace('{lastnum}',$itemv['lastnum'],$goodstitle2);
			$itemv['goodstitle2']=str_replace('{fan_fee}',$itemv['fan_price'],$goodstitle2);
		}
	}
	exi($list);
}
if($_RQ['op']=='search'){
	$setting=plugin('jy_weishop')->plugin_setting('setting');
	$return['setting']=$setting;
	$return['shopset']=plugin('jy_weishop')->plugin_setting('shopset');
	$searchkeyword=plugin('jy_weishop')->plugin_setting('search')['keyword'];
	$return['searchkeyword']=explode(';',$searchkeyword);
	$return['list']=array();
	$return['params']=array('keyword'=>'');
	$return['indexpai']=array();
	if($return['setting']['issearch']){
		$return['indexpai'][]=array('name'=>'search');
	}
	if($return['setting']['ishdps']){
		$return['indexpai'][]=array('name'=>'hdps');
	}
	if($return['setting']['isbankuai1']){
		$return['indexpai'][]=array('name'=>'bankuai1');
	}
	if($return['setting']['bankuai2']){
		$return['indexpai'][]=array('name'=>'bankuai2');
	}
	if($return['setting']['cube']){
		$return['indexpai'][]=array('name'=>'cube');
	}
	if($return['setting']['share']){
		$return['indexpai'][]=array('name'=>'share');
	}
	$return['indexpai'][]=array('name'=>'goods');
	//推荐商品
	$indexrecommands=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'saletype like'=>'%;1;%'),array('id','title','image','price','total'));
	$return['indexrecommands']=$indexrecommands;
	set_html('title',$setting['title']?'搜索-'.$setting['title']:'搜索-在线商城');
	exi($return);
}	
?>